package com.servlet.employee;

import com.bean.Employee;
import com.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/admin/updateEmployee")
public class UpdateEmployeeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        AdminService adminService=new AdminService();
        String id=req.getParameter("id");
        String name=req.getParameter("name");
        String deptid=req.getParameter("deptid");

        if (deptid.length() != 2||!adminService.judgeContainsStr(deptid)){
            req.setAttribute("Message", "部门号不满足要求，修改失败！");
            req.getRequestDispatcher("admingetfail.jsp").forward(req, resp);
            return;
        }
        boolean ismanager=req.getParameter("ismanager").equals("true")?true:false;
        Employee employee = adminService.getEmployee(id);
        employee.setName(name);
        employee.setDeptid(deptid);
        employee.setIsmanager(ismanager);

        if(!adminService.updateEmployee(employee)) {
            req.setAttribute("Message", "部门不存在，修改失败！");
            req.getRequestDispatcher("admingetfail.jsp").forward(req, resp);
        }else {
            req.setAttribute("employee", employee);
            req.setAttribute("Message", "修改成功！");
            req.getRequestDispatcher("admingetuser.jsp").forward(req, resp);
        }
    }
}
